﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace VietStar.Library.Commom.Algorithm
{
    public class Converts
    {
        public Converts()
        { }

        public string ConvertToUnsign(string UnicodeText)
        {
            for (int i = 33; i < 48; i++)
            {
                UnicodeText = UnicodeText.Replace(((char)i).ToString(), "");
            }
            for (int i = 58; i < 65; i++)
            {
                UnicodeText = UnicodeText.Replace(((char)i).ToString(), "");
            }
            for (int i = 91; i < 97; i++)
            {
                UnicodeText = UnicodeText.Replace(((char)i).ToString(), "");
            }
            for (int i = 123; i < 127; i++)
            {
                UnicodeText = UnicodeText.Replace(((char)i).ToString(), "");
            }
            UnicodeText = UnicodeText.Replace(" ", "-");
            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = UnicodeText.Normalize(System.Text.NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

        private string[] num = { "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };
        private string[] smallunit = { "linh", "lăm", "mười", "mươi", "mốt", "trăm" };
        private string[] largeunit = { "", "nghìn", "triệu", "tỷ" };
        private string[] strMainGroup;
        private string[] strSubGroup;
        private string Len1(string strA)
        {
            return num[int.Parse(strA)];
        }
        private string Len2(string strA)
        {
            if (strA.Substring(0, 1) == "0")
                return smallunit[0] + " " + Len1(strA.Substring(1, 1));
            else if (strA.Substring(0, 1) == "1")
            {
                if (strA.Substring(1, 1) == "5")
                    return smallunit[2] + " " + smallunit[1];
                else if (strA.Substring(1, 1) == "0")
                    return smallunit[2];
                else
                    return smallunit[2] + " " + Len1(strA.Substring(1, 1));
            }
            else
            {
                if (strA.Substring(1, 1) == "5")
                    return Len1(strA.Substring(0, 1)) + " " + smallunit[3] + " " + smallunit[1];
                else if (strA.Substring(1, 1) == "0")
                    return Len1(strA.Substring(0, 1)) + " " + smallunit[3];
                else if (strA.Substring(1, 1) == "1")
                    return Len1(strA.Substring(0, 1)) + " " + smallunit[3] + " " + smallunit[4];
                else
                    return Len1(strA.Substring(0, 1)) + " " + smallunit[3] + " " + Len1(strA.Substring(1, 1));
            }
        }
        private string Len3(string strA)
        {
            if ((strA.Substring(0, 3) == "000"))
                return null;
            else if ((strA.Substring(1, 2) == "00"))
                return Len1(strA.Substring(0, 1)) + " " + smallunit[5];
            else
                return Len1(strA.Substring(0, 1)) + " " + smallunit[5] + " " + Len2(strA.Substring(1, strA.Length - 1));
        }
        private string FullLen(string strSend)
        {
            bool boKTNull = false;
            string strKQ = "";
            string strA = strSend.Trim();
            int iIndex = strA.Length - 9;
            int iPreIndex = 0;

            if (strSend.Trim() == "")
                return Len1("0");
            //return zero if it is zero
            for (int i = 0; i < strA.Length; i++)
            {
                if (strA.Substring(i, 1) != "0")
                    break;
                else if (i == strA.Length - 1)
                    return num[0];
            }
            int k = 0;
            while (strSend.Trim().Substring(k++, 1) == "0")
            {
                strA = strA.Remove(0, 1);
            }
            if (strA.Length < 9)
                iPreIndex = strA.Length;
            if ((strA.Length % 9) != 0)
                strMainGroup = new string[strA.Length / 9 + 1];
            else
                strMainGroup = new string[strA.Length / 9];
            // reverse
            for (int i = strMainGroup.Length - 1; i >= 0; i--)
            {
                if (iIndex >= 0)
                {
                    iPreIndex = iIndex;
                    strMainGroup[i] = strA.Substring(iIndex, 9);
                    iIndex -= 9;
                }
                else
                    strMainGroup[i] = strA.Substring(0, iPreIndex);
            }
            // split maingroup to many subgroup
            // forward
            for (int j = 0; j < strMainGroup.Length; j++)
            {
                // set values
                iIndex = strMainGroup[j].Length - 3;
                if (strMainGroup[j].Length < 3)
                    iPreIndex = strMainGroup[j].Length;
                if ((strMainGroup[j].Length % 3) != 0)
                    strSubGroup = new string[strMainGroup[j].Length / 3 + 1];
                else
                    strSubGroup = new string[strMainGroup[j].Length / 3];
                for (int i = strSubGroup.Length - 1; i >= 0; i--)
                {
                    if (iIndex >= 0)
                    {
                        iPreIndex = iIndex;
                        strSubGroup[i] = strMainGroup[j].Substring(iIndex, 3);
                        iIndex -= 3;
                    }
                    else
                        strSubGroup[i] = strMainGroup[j].Substring(0, iPreIndex);
                }
                // get string from subgrounp
                for (int i = 0; i < strSubGroup.Length; i++)
                {
                    boKTNull = false;
                    if ((j == strMainGroup.Length - 1) && (i == strSubGroup.Length - 1))
                    {
                        if (strSubGroup[i].Length < 3)
                        {
                            if (strSubGroup[i].Length == 1)
                                strKQ += Len1(strSubGroup[i]);
                            else
                                strKQ += Len2(strSubGroup[i]);
                        }
                        else
                            strKQ += Len3(strSubGroup[i]);
                    }
                    else
                    {
                        if (strSubGroup[i].Length < 3)
                        {
                            if (strSubGroup[i].Length == 1)
                                strKQ += Len1(strSubGroup[i]) + " ";
                            else
                                strKQ += Len2(strSubGroup[i]) + " ";
                        }
                        else
                        {
                            if (Len3(strSubGroup[i]) == null)
                                boKTNull = true;
                            else
                                strKQ += Len3(strSubGroup[i]) + " ";
                        }
                    }
                    //right
                    if (!boKTNull)
                    {
                        if (strSubGroup.Length - 1 - i != 0)
                            strKQ += largeunit[strSubGroup.Length - 1 - i] + " ";
                        else
                            strKQ += largeunit[strSubGroup.Length - 1 - i] + " ";
                    }
                }
                // right
                if (j != strMainGroup.Length - 1)
                {
                    if (!boKTNull)
                        strKQ = strKQ.Substring(0, strKQ.Length - 1) + largeunit[3] + " ";
                    else
                        strKQ = strKQ.Substring(0, strKQ.Length - 1) + " " + largeunit[3] + " ";
                }
            }
            return strKQ.Trim();
        }

        public string ConvertNumberToText(string strSend)
        {
            if (strSend == "")
                return Len1("0");
            string[] strTmp = new string[2];
            try
            {
                strSend = strSend.Replace(',', '.');
                strTmp = strSend.Split('.');
                string strTmpRight = strTmp[1];
                for (int i = strTmpRight.Length - 1; i >= 0; i--)
                {
                    if (strTmpRight.Substring(i, 1) == "0")
                        strTmpRight = strTmpRight.Remove(i, 1);
                    else
                        break;
                }
                if (strTmpRight != "")
                    return FullLen(strTmp[0]) + "phẩy" + " " + FullLen(strTmp[1]).TrimEnd();
                else
                    return FullLen(strTmp[0]).TrimEnd(); ;
            }
            catch
            {
                return FullLen(strTmp[0]).TrimEnd(); ;
            }
        }

        public string ConvertMoneyToText(string money, string unit)
        {
            return ConvertNumberToText(money) + " " + unit;
        }
    }
}
